package com.dabai.applicationmanager.transfer;
 
import android.app.IntentService;
import android.content.Intent;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
 
/**
 * Created by shuangyue on 2016/9/22 9:57.
 */
 
public class DataService extends IntentService {
 
    public static final String ACTION_SEND_DATA = "send data";
    public static final String SOCKET_HOST = "socket host";
    public static final String SOCKET_PORT = "socket port";
 
 
    public DataService (String name) {
        super (name);
    }
    public DataService(){
        super("DataService");
    }
 
    @Override
    protected void onHandleIntent (Intent intent) {
        if(ACTION_SEND_DATA.equals (intent.getAction ())){
            String host = intent.getExtras ().getString (SOCKET_HOST);
            int port = intent.getExtras ().getInt (SOCKET_PORT);
            Socket socket = new Socket ();
            OutputStream os = null;
            try {
                socket.bind (null);
                socket.connect (new InetSocketAddress (host,port),5000);
                os = socket.getOutputStream ();
                os.write ("ysy".getBytes ());

            } catch (IOException e) {
                e.printStackTrace ();
            }finally {
                if(socket.isConnected ()){
                    try {
                        socket.close ();
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }

        }
    }
}